package cn.abcsys.cloud.devops.pipeline.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONObject;

import cn.abcsys.cloud.devops.pipeline.util.Result;

public class Validate {
	public static Result imageInsertObjectValidate(JSONObject paramIn){
		String targetFolder = paramIn.getString("targetFolder");
    	String fileName = paramIn.getString("fileName");
    	String imageType = paramIn.getString("imageType");
    	String registryId = paramIn.getString("registryId");
    	String envId = paramIn.getString("envId");
    	String imageCreator = paramIn.getString("userId");
    	if(isNull(targetFolder)){
    		return new Result(false, "镜像包上传路径参数不能为空");
    	}
    	if(isNull(fileName)){
    		return new Result(false, "镜像包名称参数不能为空");
    	}
    	if(isNull(imageType)||!imageType.equals(Type.IMAGE.APPLICATION.name())&&!imageType.equals(Type.IMAGE.BASIC.name())
    			&&!imageType.equals(Type.IMAGE.PUBLIC.name())){
    		return new Result(false, "镜像类型参数异常，可上传的镜像类型为："+Type.IMAGE.BASIC.name()+";"
    			+Type.IMAGE.APPLICATION.name()+";"+Type.IMAGE.PUBLIC.name());
    	}
    	if(isNull(registryId)||!isInteger(registryId)){
    		return new Result(false, "镜像仓库ID参数异常，应为正整数");
    	}
    	if(isNull(envId)||!isInteger(envId)){
    		return new Result(false, "环境ID参数异常，应为正整数");
    	}
    	if(isNull(imageCreator)||!isInteger(imageCreator)){
    		return new Result(false, "创建人ID参数异常，应为正整数");
    	}
		return new Result(true,"validate success");
	}
	/**  
    * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为2,英文字符长度为1  
    * @param String s 需要得到长度的字符串  
    * @return int 得到的字符串长度  
    */   
	public static int length(String s) {  
       if (s == null)  
           return 0;  
       char[] c = s.toCharArray();  
       int len = 0;  
       for (int i = 0; i < c.length; i++) {  
           len++;  
           if (!isLetter(c[i])) {  
               len++;  
           }  
       }  
       return len;  
	}
	public static boolean isLetter(char c) {   
	       int k = 0x80;   
	       return c / k == 0 ? true : false;   
	   }  
	  
	/** 
	 * 判断字符串是否为空 
	 * @param str 
	 * @return 
	 */  
	public static boolean isNull(String str){  
	    if(str==null||str.trim().equals("")||str.trim().equalsIgnoreCase("null")){  
	        return true;  
	    }else{  
	        return false;  
	    }  
	}
	//判断是否是一个IP
	public static boolean isIp(String IP){
		boolean b = false;
		IP = IP.trim();
		if(IP.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")){
			String s[] = IP.split("\\.");
			if(Integer.parseInt(s[0])<255)
			if(Integer.parseInt(s[1])<255)
			if(Integer.parseInt(s[2])<255)
			if(Integer.parseInt(s[3])<255)
			b = true;
		}
		return b;
	}
	/*** 
     * 判断 String 是否是 int 
     *  
     * @param input 
     * @return 
     */  
    public static boolean isInteger(String input){  
        Matcher mer = Pattern.compile("^[+-]?[0-9]+$").matcher(input);  
        return mer.find();  
    }
}
